//
//  DEWCountSafeViewController.m
//  OtherDemo
//
//  Created by lianjie on 2018/7/25.
//  Copyright © 2018年 lianjie. All rights reserved.
//

#import "DEWCountSafeViewController.h"
#import "DEWSafeTableViewCell.h"
#import "DEWSafeModel.h"
@interface DEWCountSafeViewController ()<UITableViewDataSource,UITableViewDelegate,DEWSafeCellDelegate>

@property (nonatomic ,strong) UITableView *table;

///账号安全通知
@property (nonatomic ,strong) NSMutableArray *safeList;

@property (nonatomic ,assign) NSInteger pages;

@end

@implementation DEWCountSafeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"账号安全中心";
    self.pages = 1;
    [self loadData];
    [self createView];
}
#pragma mark ----requestData
-(void)loadData
{
    weakSelf(weakSelf);
    
    NSMutableDictionary *pramseDic = [NSMutableDictionary dictionary];
    DELoginModel *model = [HHUserManager getUserInfo];
    [pramseDic setObject:model.userId forKey:@"userId"];
    [pramseDic setObject:model.tokenId forKey:@"tokenId"];
//    [pramseDic setObject:@(self.pages) forKey:@"page"];
    
    NSString *url = [KSService countSafeListAPI];
    [KSHTTPSTool post:url params:pramseDic success:^(id responseObject) {
        [BaseUtils endRefreshForScrollView:weakSelf.table];
        [BaseUtils endLoadMoreForScrollView:weakSelf.table];
        if (DERetCode) {
            
//            if (weakSelf.pages == 1) {
//                [weakSelf.safeList removeAllObjects];
//            }
            
            NSArray *arr = responseObject[@"data"];
            if (arr.count < 1) {
                [BaseUtils noticeNoMoreDataForScrollView:weakSelf.table];
            }
            
            weakSelf.safeList = [DEWSafeModel modelArrayWithDictArray:arr];
            
//            [weakSelf.safeList addObjectsFromArray:[DEWSafeModel modelArrayWithDictArray:arr]];
            [weakSelf.table reloadData];
        }else{
            [[AlertHelper sharedManager]alertWithMessage:[NSString stringWithFormat:@"%@",responseObject[@"retMsg"]]];
        }
    } failure:^(NSError *error) {
        [weakSelf showHint:@"系统异常，稍后再试！"];
    }];
    
}
#pragma mark ---initView
-(void)createView
{
    [self.view addSubview:self.table];
//    [BaseUtils addLoadMoreForScrollView:self.table loadMoreCallBack:^{
//
//        self.pages++;
//
//        [self loadData];
//    }];
    
    [BaseUtils addPullRefreshForScrollView:self.table pullRefreshCallBack:^{
        
        self.pages = 1;
        
        [self loadData];
        
    }];
    
}

#pragma mark ---safeCellAction
- (void)safeCellAtItemIndex:(NSIndexPath *)indexPath andModel:(DEWSafeModel *)model
{
    
    
}

#pragma mark ---tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.safeList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DEWSafeTableViewCell *cell = [[DEWSafeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.model = self.safeList[indexPath.row];
    cell.safeDelegate = self;
    cell.indexPath = indexPath;
    
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DEWSafeModel *model = self.safeList[indexPath.row];
    
    return model.cellHeight+Margin_15;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
}

#pragma mark --lazy
-(UITableView *)table
{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEWScreenWidth, DEWScreenHeight-(NAVH)) style:UITableViewStyleGrouped];
        
        _table.delegate = self;
        
        _table.dataSource = self;
        
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.enablePlaceHolderView = YES;
    }
    return _table;
    
}

-(NSMutableArray *)safeList
{
    if (!_safeList) {
        _safeList = [NSMutableArray array];
    }
    return _safeList;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
